package com.suyuan.controller.shop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.suyuan.entity.ProPic;
import com.suyuan.entity.ProducetypeInfo;
import com.suyuan.entity.ProductInfo;
import com.suyuan.params.CategoryParam;
import com.suyuan.service.ProductService;
import com.suyuan.utils.JsonMsg;
import com.suyuan.utils.JsonUtils;
import com.suyuan.vo.HotProductVO;
import com.suyuan.vo.ProductVO;
import com.suyuan.vo.SourceInfo;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *  商品
 */
@Controller
@RequestMapping(value = "/shop/product")
public class ProductController {

    @Resource(name = "productService")
    private ProductService productService;


    /**
     * 获取所有商品
     */
    @RequestMapping(value = "/findAll")
    @ResponseBody
    public Object findAll(@RequestBody String data){
        Page page = JsonUtils.jsonToPojo(data, Page.class);
        if(page == null){
            page = new Page();
        }
        PageInfo info = productService.findAll(page);
        return JsonMsg.SuccessMsg(info);
    }

    @RequestMapping(value = "inquiry")
    @ResponseBody
    public Object inquiry(@RequestBody String data){
        if(StringUtils.isBlank(data)){
            return JsonMsg.SuccessMsg(productService.findAll());
        }else{
            try {
                JSONObject jsonObject = JSONObject.parseObject(data);
                String productName = jsonObject.getString("productName");
                Integer pageNumber = jsonObject.getInteger("pageNumber");
                Integer pageSize = jsonObject.getInteger("pageSize");
                PageInfo info = productService.inquiry(productName,pageNumber,pageSize);
                return JsonMsg.SuccessMsg(info);
            } catch (Exception e) {
                e.printStackTrace();
                return JsonMsg.ErrorMsg("查找出错，请检查你的参数格式");
            }
        }
    }

    /**
     * 首页推荐商品
     */
    @RequestMapping(value = "/recommentPro")
    @ResponseBody
    public Object recommentPro(){
        List<ProductInfo> recomments = productService.getRecommentPro();
        return JsonMsg.SuccessMsg(recomments);
    }

    /**
     * 获取商品分类
     *
     */
    @RequestMapping(value = "/categoryList")
    @ResponseBody
    public Object categoryList(){
        List<ProducetypeInfo> list = productService.categoryList();
        return JsonMsg.SuccessMsg(list);
    }

    /**
     * 根据产品分类商品分页信息
     * @return
     */
    @RequestMapping(value = "/findByCategory")
    @ResponseBody
    public Object findByCategory(@RequestBody String data){
        CategoryParam param = JsonUtils.jsonToPojo(data, CategoryParam.class);
        if(param.getId() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        PageInfo info = productService.findByCategory(param.getId(),new Page(param.getPageNumber(),param.getPageSize()));
        return JsonMsg.SuccessMsg(info);
    }

    /**
     * 根据商品id查询详情
     */
    @RequestMapping(value = "/productDetail")
    @ResponseBody
    public Object productDetail(@RequestBody String data){
        //接收商品id
        ProductInfo info = JsonUtils.jsonToPojo(data, ProductInfo.class);
        if(info.getProductid() == null){
            return JsonMsg.ErrorMsg("请输入正确参数");
        }
        ProductVO productVO = productService.findById(info.getProductid());
        return JsonMsg.SuccessMsg(productVO);
    }

    /**
     * 根据商品id返回相关图片链接
     * 该接口暂不使用
     */
    @RequestMapping(value = "/getProPic")
    @ResponseBody
    public Object getProPic(@RequestBody String data){
        //接收商品id
        ProductInfo info = JsonUtils.jsonToPojo(data, ProductInfo.class);
        if(info.getProductid() == null){
           return JsonMsg.ErrorMsg("请输入正确参数");
        }
        List<ProPic> pices = productService.getProPic(info.getProductid());
        return JsonMsg.SuccessMsg(pices);
    }

    /**
     * 商品首页轮播图接口
     */
    @RequestMapping(value = "/slidePic")
    @ResponseBody
    public Object sildePic(){
        List<ProPic> pics = productService.getSlidePic();
        try {
            if(pics != null && pics.size() > 0){
                return JsonMsg.SuccessMsg(pics);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMsg.ErrorMsg("报异常了");
        }
        return JsonMsg.ErrorMsg("数据为空");
    }

    /**
     * 首页人们商品，格式(左边一个大图，右边两个商品详情
     */
    @RequestMapping(value = "hotProduct")
    @ResponseBody
    public Object hotProduct(){
        List<HotProductVO> vos = null;
        try {
            vos = productService.getHotProduct();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMsg.ErrorMsg("异常了");
        }
        return JsonMsg.SuccessMsg(vos);
    }

    /**
     * 商品溯源信息
     * @param traceCode
     * @return
     */
    @RequestMapping(value = "/getSourceInfo/{traceCode}",method = RequestMethod.GET)
    @ResponseBody
    public Object getSourceInfo(@PathVariable String traceCode){
        SourceInfo source = productService.getSourceInfo(traceCode);
        return JsonMsg.SuccessMsg(source);
    }
}
